/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.loof.fonzie.mock;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TemporalType;

/**
 * @author <a href="nicolas@apache.org">Nicolas De Loof</a>
 */
@SuppressWarnings( "unchecked" )
public class QueryMock
    implements Query
{
    private Object result;

    public QueryMock( Object result )
    {
        this.result = result;
    }

    public int executeUpdate()
    {
        if ( result instanceof PersistenceException )
        {
            throw (PersistenceException) result;
        }
        if ( result instanceof Collection )
        {
            return ( (Collection) result ).size();
        }
        return 1;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#getResultList()
     */
    public List getResultList()
    {
        if ( result instanceof PersistenceException )
        {
            throw (PersistenceException) result;
        }
        if ( result instanceof Collection )
        {
            return new ArrayList( (Collection) result );
        }
        return Arrays.asList( new Object[] { result } );
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#getSingleResult()
     */
    public Object getSingleResult()
    {
        if ( result instanceof PersistenceException )
        {
            throw (PersistenceException) result;
        }
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setFirstResult(int)
     */
    public Query setFirstResult( int startPosition )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setFlushMode(javax.persistence.FlushModeType)
     */
    public Query setFlushMode( FlushModeType flushMode )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setHint(java.lang.String, java.lang.Object)
     */
    public Query setHint( String hintName, Object value )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setMaxResults(int)
     */
    public Query setMaxResults( int maxResult )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(java.lang.String, java.lang.Object)
     */
    public Query setParameter( String name, Object value )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(int, java.lang.Object)
     */
    public Query setParameter( int position, Object value )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(java.lang.String, java.util.Date, javax.persistence.TemporalType)
     */
    public Query setParameter( String name, Date value, TemporalType temporalType )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(java.lang.String, java.util.Calendar, javax.persistence.TemporalType)
     */
    public Query setParameter( String name, Calendar value, TemporalType temporalType )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(int, java.util.Date, javax.persistence.TemporalType)
     */
    public Query setParameter( int position, Date value, TemporalType temporalType )
    {
        return this;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.persistence.Query#setParameter(int, java.util.Calendar, javax.persistence.TemporalType)
     */
    public Query setParameter( int position, Calendar value, TemporalType temporalType )
    {
        return this;
    }

}
